package cn.edu.hunau.controller;

import cn.edu.hunau.service.IndexService;
import cn.edu.hunau.service.OrderService;
import cn.edu.hunau.service.impl.IndexServiceImpl;
import cn.edu.hunau.service.impl.OrderServiceImpl;
import cn.edu.hunau.utils.JsonUtils;
import cn.edu.hunau.utils.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 苏佳
 * @Date 2022 12 04 10 32
 **/
@WebServlet("*.index")
public class IndexController extends HttpServlet {
    private IndexService indexService=new IndexServiceImpl();
    private OrderService orderService=new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String matchValue = req.getHttpServletMapping().getMatchValue();
        PrintWriter out = resp.getWriter();
        if("load".matches(matchValue)){
            Map<String, Object> datas = indexService.loadIndexData();
            out.println(JsonUtils.objectToJson(R.ok().data(datas)));
        } else if ("loadOrderNum".matches(matchValue)) {
            String uid = req.getParameter("uid");
            int id = Integer.parseInt(uid);
            //状态结果
            int count1 = orderService.countByOstate(id,"1");
            int count2 = orderService.countByOstate(id,"2");
            int count3 = orderService.countByOstate(id,"3");
            int count4 = orderService.countByOstate(id,"4");
            List<Integer> list = new ArrayList<Integer>();
            list.add(count1);
            list.add(count2);
            list.add(count3);
            list.add(count4);
            out.println(JsonUtils.objectToJson(R.ok().data(list)));
        }
    }
}
